package com.ruoyi.canteen.controller.v1;

import com.ruoyi.canteen.service.WxAuthenticationService;
import com.ruoyi.canteen.utils.JwtToken;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author Ryan
 * @Date 2021/9/19 16:52
 * @Version 1.0
 */
@RequestMapping(value = "/v1/token")
@RestController
public class TokenController {

    @Autowired
    private WxAuthenticationService wxAuthenticationService;

    @PostMapping("/getToken/{account}")
    public Map<String, String> getToken(@PathVariable("account") String account) {
        Map<String, String> map = new HashMap<>();
        String token = wxAuthenticationService.code2Session(account);
        map.put("token", token);
        return map;
    }

    @PostMapping("/verify/{token}")
    public Map<String, Boolean> verify(@PathVariable("token") String token) {
        Map<String, Boolean> map = new HashMap<>();
        Boolean valid = JwtToken.verifyToken(token);
        map.put("is_valid", valid);
        return map;
    }

    @GetMapping("/getWxXcxAccessToken")
    public Map<String, Object> getAccessTokenMap(){
        Map<String, Object> map = new HashMap<>();
        map = wxAuthenticationService.getQyAccessToken();
//        if(){
//        }
//        map.put("xcxToken", wxAuthenticationService.getAccessTokenMap());
        return map;
    }

    @GetMapping("/getWeCardToken")
    public Map<String, Object> getWeCardToken(String wxCode){
        Map<String, Object> map = wxAuthenticationService.getWeCardToken(wxCode);
        Map<String, Object> resultMap = new HashMap<String, Object>();
                String wkToken = "";
        if(!map.isEmpty()){
            String token = map.get("access_token").toString();
            resultMap.put("accessToken", token);
            wkToken = wxAuthenticationService.getWeCardUserInfo(token);
        }
        resultMap.put("wkToken", wkToken);
        return resultMap;
    }

    @GetMapping(value = "/getQyWx")
    public AjaxResult getQyWx(@RequestParam String code){
//        WxCpService wxCpService = new WxCpServiceImpl();
        Map map = wxAuthenticationService.getQyAccessToken();
        String accessToken = map.get("access_token").toString();
        if(StringUtils.isEmpty(accessToken)){
            return AjaxResult.error(map.get("errmsg").toString());
        }else{
            String userId = wxAuthenticationService.getQyUserId(accessToken, code);
            if(StringUtils.isEmpty(userId)){
                String token = wxAuthenticationService.getUserInfoByQy(accessToken, userId);
                return AjaxResult.success(token);
            }
        }
        return AjaxResult.error();
    }

}
